package com.aceitunaproject.server.equipment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.aceitunaproject.server.ServerConfiguration;

/**
 * Static class to manage the Equipment classes, needed because they are not
 * managed objects.
 * 
 * If the Equipments are saved as managed objects in the dstar object store,
 * each time they are consulted an extra layer will be involved. since there is
 * no need to save them there (as they are not changed by the game, they are
 * taken from configuration files or similar sources), only the ids of the
 * Equipments will be saved in the Managed Objects, the Equipment manager will be
 * called by them to obtain the full Equipment class instance.
 * 
 * @author Federico Bergstein
 * 
 */
public class EquipmentManagerImpl implements EquipmentManager {

	// All existent Equipments
	private static Map<String, Equipment> allEquipments;

	// A class responsible for obtaining the Equipments from the source
	// (configuration file, web service, etc)
	private static EquipmentLoader equipmentLoader;

	
	
	
	//resets the class 
	public void reset()
	{
		allEquipments = null;
		equipmentLoader = null;
	
	}
	
	
	/**
	 * Gets a equipment loader from the ServerConfiguration if it is null (and
	 * sets its value in the equipmentLoader field), else returns the
	 * equipmentLoader field value
	 * 
	 * @return a Equipment Loader
	 */
	private static EquipmentLoader getEquipmentLoader() {
		if (equipmentLoader == null) {
			equipmentLoader = ServerConfiguration.getEquipmentLoader();

		}
		return equipmentLoader;
	}

	/**
	 * Gets all existent Equipments
	 * 
	 * @return A map of all Equipments
	 */
	public Map<String, Equipment> getAllEquipments() {
		if (allEquipments == null && getEquipmentLoader() != null) {

			allEquipments = getEquipmentLoader().getEquipments();
		}

		return allEquipments;
	}

	/**
	 * From a given list of equipment IDs, returns a map with the actual
	 * objects.
	 * 
	 * @param equipmentIDs
	 *            A list of the IDs of the Equipment instances to retrieve
	 * @return A map with the Equipment Instances with the received IDs
	 */
	public Map<String, Equipment> getEquipmentCollection(
			List<String> EquipmentIDs) {

		Map<String, Equipment> map = new HashMap<String, Equipment>();
		if (EquipmentIDs != null) {
			for (String id : EquipmentIDs) {
				map.put(id, getAllEquipments().get(id));
			}
		}

		return map;
	}

	

}
